import { BasicColumn, FormSchema } from '@apps/antd-admin/src/components/table';
import { h } from 'vue';
import { render } from '@apps/antd-admin/src/hooks/render/renderUtils';
import { Switch, Tag } from 'ant-design-vue';
import { weChatList } from '@apps/ant-bus/src/views/business/platformManage/wechat/data/api';
import { categoryList, setFirstRecommend, vipcardCategoryList } from './api';
import { goodsList, setIsShelf, setIsShowShop } from '@apps/ant-bus/src/views/business/goods/goods/api';
import { useMessage } from '@apps/antd-admin/src/hooks/web/useMessage';
import { rules } from '@pkg/utils';

export const columns: BasicColumn[] = [
  {
    title: '卡种名称',
    dataIndex: 'goodsName',
    width: 200,
  },
  {
    title: '卡类型',
    dataIndex: 'categoryName',
    width: 70,
  },
  {
    title: '卡分类',
    dataIndex: 'vipcardCategoryName',
    width: 70,
  },
  {
    title: '卡种编号',
    dataIndex: 'vipCardNumber',
    width: 120,
  },
  {
    title: '卡图片',
    dataIndex: 'coverImg',
    width: 90,
    customRender: render.renderImage,
  },
  {
    title: '会员卡价格',
    dataIndex: 'goodsPrice',
    width: 90,
  },
  {
    title: '续卡卡种',
    dataIndex: 'isContinue',
    width: 90,
    customRender: ({ record }: Recordable) => {
      const isContinue = record.isContinue;
      const color = isContinue == 1 ? 'green' : 'orange';
      const text = isContinue == 1 ? '是' : '不是';
      return h(Tag, { color: color }, () => text);
    },
  },
  {
    title: '续卡价格',
    dataIndex: 'newPrice',
    width: 90,
  },
  {
    title: '上下架',
    dataIndex: 'isShelf',
    width: 100,
    customRender: ({ record }: Recordable) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.isShelf,
        checkedValue: 1,
        unCheckedValue: 2,
        checkedChildren: '已上架',
        unCheckedChildren: '已下架',
        loading: record.pendingStatus,
        onChange(checked: number) {
          console.info(checked);
          record.pendingStatus = false;
          const { createMessage } = useMessage();
          setIsShelf(record.vipcardId, checked)
            .then(data => {
              record.isShelf = checked;
            })
            .catch(data => {
              console.info('catch', data);
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },
  {
    title: '卡种列表展示',
    dataIndex: 'firstRecommend',
    width: 120,
    customRender: ({ record }: Recordable) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.firstRecommend,
        checkedValue: 1,
        unCheckedValue: 2,
        checkedChildren: '展示',
        unCheckedChildren: '不展示',
        loading: record.pendingStatus,
        onChange(checked: number) {
          console.info(checked);
          record.pendingStatus = false;
          setFirstRecommend(record.vipcardId, checked)
            .then(data => {
              record.firstRecommend = checked;
            })
            .catch(data => {
              console.info('catch', data);
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },
  {
    title: '商城展示',
    dataIndex: 'isShowShop',
    width: 100,
    customRender: ({ record }: Recordable) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.isShowShop,
        checkedValue: 1,
        unCheckedValue: 2,
        checkedChildren: '展示',
        unCheckedChildren: '不展示',
        loading: record.pendingStatus,
        onChange(checked: number) {
          record.pendingStatus = false;
          setIsShowShop(record.vipcardId, checked)
            .then(data => {
              record.isShowShop = checked;
            })
            .catch(data => {
              console.info('catch', data);
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },
  {
    title: '计费方式',
    dataIndex: 'billingWay',
    width: 100,
    customRender: ({ record }: Recordable) => {
      const billingWay = record.billingWay;
      const color = billingWay == 1 ? 'green' : 'orange';
      const text = billingWay == 1 ? '期间计费' : '计次计费';
      return h(Tag, { color: color }, () => text);
    },
  },
  {
    title: '是否体验',
    dataIndex: 'isTiyan',
    width: 80,
    customRender: ({ record }: Recordable) => {
      const isTiyan = record.isTiyan;
      const color = isTiyan == 1 ? 'green' : 'orange';
      const text = isTiyan == 1 ? '体验卡' : '正式卡';
      return h(Tag, { color: color }, () => text);
    },
  },
  {
    title: '创建日期',
    dataIndex: 'createTime',
    width: 150,
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'goodsName',
    label: '卡种名称',
    component: 'Input',
    colProps: { span: 5 },
  },
  {
    field: 'vipCardNumber',
    label: '卡种编号',
    component: 'Input',
    colProps: { span: 5 },
  },
  {
    field: 'isShelf',
    label: '是否上架',
    component: 'Select',
    colProps: { span: 5 },
    componentProps: {
      options: [
        { label: '上架', value: 1 },
        { label: '下架', value: 2 },
      ],
    },
  },
  {
    field: 'categoryId',
    label: '卡类型',
    component: 'ApiSelect',
    colProps: { span: 5 },
    componentProps: {
      api: categoryList,
      params: { handlerName: 'busCategoryListHandler' },
      labelField: 'categoryName',
      valueField: 'categoryId',
      immediate: true,
    },
  },
  {
    field: 'vipcardCategoryId',
    label: '卡分类',
    component: 'ApiSelect',
    colProps: { span: 5 },
    componentProps: {
      api: vipcardCategoryList,
      params: {},
      labelField: 'vipcardCategoryName',
      valueField: 'vipcardCategoryId',
      immediate: true,
    },
  },
  {
    field: 'isTiyan',
    label: '是否体验',
    component: 'Select',
    colProps: { span: 5 },
    componentProps: {
      options: [
        { label: '体验卡', value: 1 },
        { label: '正式卡', value: 0 },
      ],
    },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'divider1',
    label: '会员卡基础信息',
    component: 'Divider',
    helpMessage: '会员卡基础信息填写',
  },
  {
    label: '卡种Id',
    field: 'vipcardId',
    component: 'Input',
    required: false,
    show: false,
  },
  {
    label: '卡名称',
    field: 'goodsName',
    component: 'Input',
    colProps: { span: 8 },
    required: true,
  },
  {
    label: '卡价格',
    field: 'goodsPrice',
    component: 'InputNumber',
    colProps: { span: 8 },
    required: true,
  },
  {
    label: '卡类型',
    field: 'categoryId',
    component: 'ApiSelect',
    colProps: { span: 8 },
    required: true,
    componentProps: {
      api: categoryList,
      params: { handlerName: 'busCategoryListHandler' },
      labelField: 'categoryName',
      valueField: 'categoryId',
      immediate: true,
    },
  },
  {
    label: '卡分类',
    field: 'vipcardCategoryId',
    component: 'ApiSelect',
    colProps: { span: 8 },
    required: true,
    componentProps: {
      api: vipcardCategoryList,
      labelField: 'vipcardCategoryName',
      valueField: 'vipcardCategoryId',
    },
  },
  {
    label: '带人设置',
    field: 'isTakePeople',
    component: 'Select',
    required: true,
    colProps: { span: 8 },
    defaultValue: 2,
    componentProps: {
      options: [
        { label: '可以带人', value: 1 },
        { label: '不可以带人', value: 2 },
      ],
    },
  },
  {
    label: '体验卡种',
    field: 'isTiyan',
    component: 'Select',
    labelWidth: '100px',
    colProps: { span: 8 },
    required: true,
    defaultValue: 0,
    componentProps: {
      options: [
        { label: '正式卡', value: 0 },
        { label: '体验卡', value: 1 },
      ],
    },
  },
  {
    label: '库存',
    field: 'inventory',
    component: 'InputNumber',
    colProps: { span: 6 },
    required: true,
  },
  {
    label: '销量',
    field: 'sales',
    component: 'InputNumber',
    colProps: { span: 6 },
    required: true,
  },
  {
    label: '排序',
    field: 'vipcardSort',
    component: 'InputNumber',
    colProps: { span: 6 },
    required: true,
  },
  {
    label: '商城展示',
    field: 'isShowShop',
    component: 'Select',
    labelWidth: '100px',
    colProps: { span: 8 },
    required: true,
    defaultValue: 2,
    componentProps: {
      options: [
        { label: '不展示', value: 2 },
        { label: '展示', value: 1 },
      ],
    },
  },
  {
    label: '公众号',
    field: 'useWeChatId',
    component: 'ApiSelect',
    colProps: { span: 8 },
    required: true,
    componentProps: {
      api: weChatList,
      labelField: 'weChatName',
      valueField: 'weChatId',
      immediate: true,
    },
  },
  {
    label: '扫码说明',
    field: 'useDesc',
    colProps: { span: 8 },
    component: 'InputTextArea',
    required: true,
  },
  {
    label: '封面图',
    field: 'coverImg',
    colProps: { span: 8 },
    component: 'ImageUpload',
    required: false,
  },
  {
    label: '详情图',
    colProps: { span: 8 },
    field: 'detailsImg',
    component: 'ImageUpload',
    required: false,
  },
  {
    field: 'divider2',
    label: '会员卡设置',
    component: 'Divider',
    helpMessage: '会员卡设置',
  },
  {
    label: '推荐卡种',
    field: 'firstRecommend',
    component: 'Select',
    colProps: { span: 8 },
    required: true,
    defaultValue: 2,
    componentProps: {
      options: [
        { label: '不推荐', value: 2 },
        { label: '推荐', value: 1 },
      ],
    },
  },
  {
    label: '首页推荐',
    field: 'isHomeRecommend',
    component: 'Select',
    colProps: { span: 8 },
    required: true,
    defaultValue: 2,
    componentProps: {
      options: [
        { label: '不推荐', value: 2 },
        { label: '推荐', value: 1 },
      ],
    },
  },
  {
    label: '商城推荐',
    field: 'isHomeRecommend',
    component: 'Select',
    colProps: { span: 8 },
    required: true,
    defaultValue: 2,
    show: false,
    componentProps: {
      options: [
        { label: '不推荐', value: 2 },
        { label: '推荐', value: 1 },
      ],
    },
  },
  {
    label: '有效期',
    field: 'isHaveValidDay',
    component: 'Select',
    colProps: { span: 8 },
    required: true,
    defaultValue: 2,
    componentProps: ({ formActionType }) => {
      return {
        options: [
          { label: '终身有效', value: 2 },
          { label: '有有效期', value: 1 },
        ],
        onChange: isHaveValidDay => {
          console.info('isHaveValidDay', isHaveValidDay);
          const { updateSchema } = formActionType;
          updateSchema([
            {
              field: 'validDay',
              ifShow: true,
            },
          ]);
        },
      };
    },
  },
  {
    label: '有效期(天)',
    field: 'validDay',
    component: 'InputNumber',
    colProps: { span: 8 },
    required: true,
    ifShow: false,
  },
  {
    label: '数量限制',
    field: 'isLimitNumber',
    component: 'Select',
    colProps: { span: 8 },
    required: true,
    defaultValue: 1,
    componentProps: {
      options: [
        { label: '不限制', value: 2 },
        { label: '限制', value: 1 },
      ],
    },
  },
  {
    label: '续卡卡种',
    field: 'isContinue',
    component: 'Select',
    colProps: { span: 8 },
    required: true,
    defaultValue: 2,
    componentProps: ({ formActionType }) => {
      return {
        options: [
          { label: '不是', value: 2 },
          { label: '是', value: 1 },
        ],
        onChange: isContinue => {
          console.info('isContinue', isContinue);
          const { updateSchema } = formActionType;
          //所属部门修改后更新负责部门下拉框数据
          updateSchema([
            {
              field: 'newPrice',
              ifShow: true,
            },
          ]);
        },
      };
    },
  },
  {
    label: '续卡价格',
    field: 'newPrice',
    component: 'InputNumber',
    colProps: { span: 6 },
    required: true,
    ifShow: false,
  },
  {
    label: '计费方式',
    field: 'billingWay',
    component: 'Select',
    colProps: { span: 8 },
    required: true,
    defaultValue: 1,
    componentProps: {
      options: [
        { label: '按次计费', value: 2 },
        { label: '按期间计费', value: 1 },
      ],
    },
  },
  {
    field: 'divider3',
    label: '卡号规则设置',
    component: 'Divider',
    helpMessage: '卡号规则设置',
  },
  {
    label: '卡号前缀',
    field: 'prefixNo',
    component: 'Input',
    colProps: { span: 8 },
    required: true,
  },
  {
    label: '卡号后缀',
    field: 'suffixNo',
    component: 'Input',
    colProps: { span: 8 },
    required: true,
  },
  {
    label: '中间位数',
    field: 'commonNo',
    component: 'InputNumber',
    colProps: { span: 8 },
    required: true,
    helpMessage: '公共部分有几个0',
  },
  {
    label: '带区域号',
    field: 'isContainArea',
    component: 'Select',
    required: true,
    colProps: { span: 8 },
    defaultValue: 1,
    componentProps: {
      options: [
        { label: '不带区域号', value: 2 },
        { label: '带区域号', value: 1 },
      ],
    },
  },
  {
    field: 'divider4',
    label: '同步信息设置',
    component: 'Divider',
    helpMessage: '同步信息到机票系统',
  },
  {
    label: '是否同步',
    field: 'isSyncVip',
    component: 'Select',
    required: true,
    defaultValue: 1,
    colProps: { span: 8 },
    componentProps: {
      options: [
        { label: '不同步到订票系统', value: 0 },
        { label: '同步到订票系统', value: 1 },
      ],
    },
  },
  {
    label: '会员卡协议',
    field: 'agreement',
    component: 'TyEditor',
  },
  /* {
    label: '会员卡简介',
    field: 'goodsDesc',
    component: 'TyEditor',
  },*/
];

export const vipcardCommonFormSchema: FormSchema[] = [
  {
    label: '卡种Id',
    field: 'vipcardId',
    component: 'Input',
    required: false,
    show: false,
  },
  {
    label: '卡名称',
    field: 'goodsName',
    component: 'Input',
    required: true,
  },
  {
    label: '卡价格',
    field: 'goodsPrice',
    component: 'InputNumber',
    required: true,
    dynamicRules: () => rules.price(true),
  },
  {
    label: '卡类型',
    field: 'categoryId',
    component: 'ApiSelect',
    required: true,
    componentProps: {
      api: categoryList,
      params: { handlerName: 'busCategoryListHandler' },
      labelField: 'categoryName',
      valueField: 'categoryId',
      immediate: true,
    },
  },
  {
    label: '卡分类',
    field: 'vipcardCategoryId',
    component: 'ApiSelect',
    required: true,
    componentProps: {
      api: vipcardCategoryList,
      labelField: 'vipcardCategoryName',
      valueField: 'vipcardCategoryId',
    },
  },

  {
    label: '带人设置',
    field: 'isTakePeople',
    component: 'Select',
    required: true,
    defaultValue: 2,
    componentProps: {
      options: [
        { label: '可以带人', value: 1 },
        { label: '不可以带人', value: 2 },
      ],
    },
  },
  {
    label: '体验卡种',
    field: 'isTiyan',
    component: 'Select',
    labelWidth: '100px',
    required: true,
    defaultValue: 0,
    componentProps: {
      options: [
        { label: '正式卡', value: 0 },
        { label: '体验卡', value: 1 },
      ],
    },
  },
  {
    label: '库存',
    field: 'inventory',
    component: 'InputNumber',
    required: true,
  },
  {
    label: '销量',
    field: 'sales',
    component: 'InputNumber',
    required: true,
  },
  {
    label: '排序',
    field: 'vipcardSort',
    component: 'InputNumber',
    required: true,
  },
  {
    label: '商城展示',
    field: 'isShowShop',
    component: 'Select',
    labelWidth: '100px',
    required: true,
    defaultValue: 2,
    componentProps: {
      options: [
        { label: '不展示', value: 2 },
        { label: '展示', value: 1 },
      ],
    },
  },
  {
    label: '售卖公众号',
    field: 'useWeChatId',
    component: 'ApiSelect',
    required: true,
    componentProps: {
      api: weChatList,
      labelField: 'weChatName',
      valueField: 'weChatId',
      immediate: true,
    },
  },
  {
    label: '是否同步',
    field: 'isSyncVip',
    component: 'Select',
    required: true,
    defaultValue: 1,
    componentProps: {
      options: [
        { label: '不同步到订票系统', value: 0 },
        { label: '同步到订票系统', value: 1 },
      ],
    },
  },
  {
    label: '扫码说明',
    field: 'useDesc',
    component: 'InputTextArea',
    required: true,
  },
  {
    label: '封面图',
    field: 'coverImg',
    component: 'ImageUpload',
    required: false,
  },
  {
    label: '详情图',
    field: 'detailsImg',
    component: 'ImageUpload',
    required: false,
  },
];

export const vipcardSetFormSchema: FormSchema[] = [
  {
    label: '推荐卡种',
    field: 'firstRecommend',
    component: 'Select',
    required: true,
    defaultValue: 2,
    componentProps: {
      options: [
        { label: '不推荐', value: 2 },
        { label: '推荐', value: 1 },
      ],
    },
  },
  {
    label: '商城展示',
    field: 'isShowShop',
    component: 'Select',
    required: false,
    defaultValue: 2,
    componentProps: {
      options: [
        { label: '不展示', value: 2 },
        { label: '展示', value: 1 },
      ],
    },
  },
  {
    label: '有效期',
    field: 'isHaveValidDay',
    component: 'Select',
    required: true,
    defaultValue: 2,
    componentProps: {
      options: [
        { label: '终身有效', value: 2 },
        { label: '有有效期', value: 1 },
      ],
    },
  },
  {
    label: '有效期(天)',
    field: 'validDay',
    component: 'InputNumber',
    required: true,
    ifShow: ({ model }) => {
      if (model.isHaveValidDay == 1) {
        return true;
      } else {
        return false;
      }
    },
  },
  {
    label: '数量限制',
    field: 'isLimitNumber',
    component: 'Select',
    required: true,
    defaultValue: 1,
    componentProps: {
      options: [
        { label: '不限制', value: 2 },
        { label: '限制', value: 1 },
      ],
    },
  },
  {
    label: '续卡卡种',
    field: 'isContinue',
    component: 'Select',
    required: true,
    defaultValue: 2,
    componentProps: {
      options: [
        { label: '不是', value: 2 },
        { label: '是', value: 1 },
      ],
    },
  },
  {
    label: '续卡价格',
    field: 'newPrice',
    component: 'InputNumber',
    required: true,
    dynamicRules: () => rules.price(true),
    ifShow: ({ model }) => {
      if (model.isContinue == 1) {
        return true;
      } else {
        return false;
      }
    },
  },
  {
    label: '计费方式',
    field: 'billingWay',
    component: 'Select',
    required: true,
    defaultValue: 1,
    componentProps: {
      options: [
        { label: '按次计费', value: 2 },
        { label: '按期间计费', value: 1 },
      ],
    },
  },
];

export const vipcardNoFormSchema: FormSchema[] = [
  {
    label: '卡种Id',
    field: 'vipcardId',
    component: 'Input',
    required: false,
    show: false,
  },
  {
    label: '生成方式',
    field: 'vipcardNoType',
    component: 'Select',
    required: true,
    defaultValue: 0,
    dynamicDisabled: ({ model }) => {
      if (model.vipcardId) {
        return true;
      } else {
        return false;
      }
    },
    componentProps: ({ formModel }) => {
      return {
        options: [
          { label: '系统生成', value: 0 },
          { label: '完善资料生成', value: 1 },
        ],
        onChange(vipcardNoType) {
          formModel.prefixNo = null;
          formModel.suffixNo = null;
          formModel.commonNo = null;
          formModel.isContainArea = null;
        },
      };
    },
  },
  {
    label: '卡号前缀',
    field: 'prefixNo',
    component: 'Input',
    required: true,
    ifShow: ({ model }) => {
      if (model.vipcardNoType == 0) {
        return true;
      } else {
        return false;
      }
    },
    dynamicDisabled: ({ model }) => {
      if (model.vipcardId) {
        return true;
      } else {
        return false;
      }
    },
  },
  {
    label: '卡号后缀',
    field: 'suffixNo',
    component: 'Input',
    required: true,
    ifShow: ({ model }) => {
      if (model.vipcardNoType == 0) {
        return true;
      } else {
        return false;
      }
    },
    dynamicDisabled: ({ model }) => {
      if (model.vipcardId) {
        return true;
      } else {
        return false;
      }
    },
  },
  {
    label: '中间位数',
    field: 'commonNo',
    component: 'InputNumber',
    required: true,
    ifShow: ({ model }) => {
      if (model.vipcardNoType == 0) {
        return true;
      } else {
        return false;
      }
    },
    dynamicDisabled: ({ model }) => {
      if (model.vipcardId) {
        return true;
      } else {
        return false;
      }
    },
    helpMessage: '公共部分有几个0',
  },
  {
    label: '带区域号',
    field: 'isContainArea',
    component: 'Select',
    required: true,
    defaultValue: 1,
    ifShow: ({ model }) => {
      if (model.vipcardNoType == 0) {
        return true;
      } else {
        return false;
      }
    },
    dynamicDisabled: ({ model }) => {
      if (model.vipcardId) {
        return true;
      } else {
        return false;
      }
    },
    componentProps: {
      options: [
        { label: '不带区域号', value: 2 },
        { label: '带区域号', value: 1 },
      ],
    },
  },
];

export const agreementFormSchema: FormSchema[] = [
  {
    label: '协议简介',
    field: 'goodsDesc',
    component: 'TyEditor',
  },
];

export const vipcardCopyFormSchema: FormSchema[] = [
  {
    label: '要复制卡种',
    field: 'vipcardId',
    component: 'ApiSelect',
    required: true,
    componentProps: ({ formModel }) => {
      return {
        api: goodsList,
        params: { handlerName: 'busGoodsVipCardListHandler' },
        labelField: 'goodsName',
        valueField: 'goodsId',
        immediate: false,
        showSearch: true,
      };
    },
  },
  {
    label: '生成方式',
    field: 'vipcardNoType',
    component: 'Select',
    required: true,
    defaultValue: 0,
    componentProps: ({ formModel }) => {
      return {
        options: [
          { label: '系统生成', value: 0 },
          { label: '完善资料生成', value: 1 },
        ],
      };
    },
    dynamicDisabled: true,
  },
  {
    label: '卡号前缀',
    field: 'prefixNo',
    component: 'Input',
    required: true,
  },
  {
    label: '卡号后缀',
    field: 'suffixNo',
    component: 'Input',
    required: true,
  },
  {
    label: '中间位数',
    field: 'commonNo',
    component: 'InputNumber',
    required: true,
    helpMessage: '公共部分有几个0',
    componentProps: {
      style: { width: '150px' },
    },
  },
  {
    label: '带区域号',
    field: 'isContainArea',
    component: 'Select',
    required: true,
    defaultValue: 1,
    componentProps: {
      options: [
        { label: '不带区域号', value: 2 },
        { label: '带区域号', value: 1 },
      ],
    },
  },
];

export const vipcardCopyUseAirportFormSchema: FormSchema[] = [
  {
    label: '卡种Id',
    field: 'vipcardId',
    component: 'Input',
    required: false,
    show: false,
  },
  {
    label: '要复制卡种',
    field: 'copyVipcardId',
    component: 'ApiSelect',
    required: true,
    componentProps: ({ formModel }) => {
      return {
        api: goodsList,
        params: { handlerName: 'busGoodsVipCardListHandler' },
        labelField: 'goodsName',
        valueField: 'goodsId',
        immediate: false,
        showSearch: true,
      };
    },
  },
];
